<?php
/**
 * Description of acl
 *
 * @author Gmax
 */
class System_App_Acl extends System_App_Noyau{
    private $datas;
    
    function __construct() {
        $this->register('acl');
        $src = APP_PATH.'config/acl.ini';
        if( file_exists($src) ){
            $this->datas = parse_ini_file($src, true);
        }
        
    }
    
    public function checkAcl( $role, $config ){
        $controller = $config['controller'];
        $action = $config['action'];
        $module = $config['module'];
        
        $access = true;
        if( isset( $this->datas[$role] ) && count( $this->datas[$role] ) > 0 ){
            $acl = $this->datas[$role];
            foreach( $acl as $droits => $bool ){
                $split = explode('.', $droits );
                
                if( count($split) === 3 ){
                    $acl_module = '';
                    $acl_controller = $split[0];
                    $acl_access = $split[1];
                    $acl_target = $split[2];
                }elseif( count($split) === 4 ){
                    $acl_module = $split[0];
                    $acl_controller = $split[1];
                    $acl_access = $split[2];
                    $acl_target = $split[3];
                }
                
                if(  $acl_module === $module ){
                    if( $acl_controller === 'all' || $acl_controller === $controller ){
                        if( $acl_target === 'all' || $acl_target === $action ){
                            switch( $acl_access ){
                                case 'allow':
                                    $access = true;
                                    break;
                                case 'deny':
                                    $access = false;
                                    break;
                            }
                            break;
                        }
                    }
                }
            }
        }
        return $access;
    }
}

?>